﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using Castle.Core.Logging;
using FwProject.Authorization.Users;
using FwProject.Comm;
using FwProject.Core.Distribution;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.JPush.JPushModel;
using FwProject.PrePaymentInfo;
using FwProject.PublicService.JPush;
using FwProject.Restaurant;
using FwProject.Users;
using Microsoft.EntityFrameworkCore;

namespace FwProject.RemoteEventBus
{
    /// <summary>
    /// ReceiptInfoHandle 通知修改缴款状态
    /// </summary>

    [RemoteEventHandler(ForType = RemoteTypeHelp.TypeReceiptInfoModelBySeletlmentInfo, ForTopic = RemoteTypeHelp.TopicReceiptInfoModelBySeletlmentInfo)]
    public class ReceiptInfoModelBySettlementIfno : IRemoteEventHandler, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public readonly IJPushAppservice _jPushAppservice;
    
        public readonly IRepository<ReceiptInfoBySettlementModel, Guid> _repositoryReceiptInfoBySettlementModel;
        public readonly IRepository<SettlementInfo, Guid> _repositorySettlementInfo;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        public ILogger _logger { get; set; }
        public ReceiptInfoModelBySettlementIfno(ICacheManager catchmanager, IJPushAppservice jPushAppservice,
                                      IRepository<ReceiptInfoBySettlementModel, Guid> repositoryReceiptInfoBySettlementModel,
                           IRepository<SettlementInfo, Guid> repositorySettlementInfo,
                                         IUnitOfWorkManager unitOfWorkManager
                                          )
        {
            _repositoryReceiptInfoBySettlementModel = repositoryReceiptInfoBySettlementModel;
            _repositorySettlementInfo = repositorySettlementInfo;
            _catchmanager = catchmanager;
            _jPushAppservice = jPushAppservice;
         
            _logger = NullLogger.Instance;
            _unitOfWorkManager = unitOfWorkManager;

        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                var Id = Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString());
                try
                {
                    ///管理数据
                    var model = _repositoryReceiptInfoBySettlementModel.GetAll().Where(o => o.ReceiptInfoId == Id && o.ToVoidStatus != ToVoidStatus.已作废);
                        foreach (var item in model)
                        {
                            var m = _repositorySettlementInfo.Get(item.SettlementId);
                            if (m.Actualprice == (m.ReceivedDiscount + m.ReceivedPrice + m.UsePrePayment))
                                m.RestaurantPayStatus = RestaurantPayStatus.已收完款;
                        }
                    _logger.Info($"收款编号{Id}");
                }
                catch (Exception exe)
                {

                    _logger.Error($"收款编号{ Id}同步收据状态失败{nameof(ReceiptInfoModelBySettlementIfno)}", exe);
                }
                finally
                {
                    _logger.Info($"收款编号{Id}完成处理{nameof(ReceiptInfoModelBySettlementIfno)}");
                }

                unitOfWork.Complete();
            }
        }
    }

}
